//
//  OrderDetailForTable.m
//  ServiceApp
//
//  Created by User-23 on 12/10/14.
//  Copyright (c) 2014 TrainingCamp. All rights reserved.
//

#import "OrderDetailForTable.h"
#import "TablesManager.h"
#import "BillsManager.h"
#import "Product.h"

@interface OrderDetailForTable ()

@property Table* selectedTable;
@property NSTimer* updateTimer;

@end

@implementation OrderDetailForTable

//Disable timer for automatic Bill Update
-(void)viewWillDisappear:(BOOL)animated{
    [self.updateTimer invalidate];
    self.updateTimer = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
        [[[[self.tabBarController tabBar]items]objectAtIndex:0]setEnabled:NO];
    //Timer for automatic bill update
    self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(update) userInfo:nil repeats:YES];
    
    //Show navigation bar
    self.navigationController.navigationBar.hidden = NO;
    
    //Get currently selected table
    self.selectedTable = [[TablesManager sharedTables] selectedTable];
    
    //Get bill for table
    [BillsManager getBillForTable:self.selectedTable];
    
    //Show edit button
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
}

#pragma mark - Table View Data Source

- (void) update{
    if (![self.tableView isEditing]){
        [BillsManager getBillForTable:self.selectedTable];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.title = [NSString stringWithFormat:@"%.2f lv.", self.selectedTable.bill.totalBill];
            [self.tableView reloadData];
        });
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
        return [self.selectedTable.bill count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"basicCell" forIndexPath:indexPath];
    
    // Configure the cell...
    Product* product = [self.selectedTable.bill orderAtIndex:indexPath.row];
    cell.textLabel.text = [product name];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f lv.", [product price]];

    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}





// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [self.selectedTable.bill deleteOrderAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
